import { ALL, Body, Inject, Post, Provide } from '@midwayjs/decorator';
import { CoolController, BaseController, BaseService } from 'midwayjs-cool-core';
import { Context } from 'vm';
import { courseSourceEntity } from '../../../courseSource/entity/courseSource';
import { courseSystemEntity } from '../../../courseSystem/entity/courseSystem';
import { teacherEntity } from '../../../teacher/entity/teacher';
import { classTeachingEntity  } from '../../entity/classTeaching';
import { studentClassDetailEntity } from '../../entity/studentClassDetail';
import { classTeachingService } from '../../service/classTeaching';

/**
 * 描述
 */
@Provide()
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: classTeachingEntity,
  service:classTeachingService,
  before: ctx => {
    // 将前端的数据转JSON格式存数据库
    // const { data } = ctx.request.body;
    let data = ctx.request.body;

    ctx.request.body.data = JSON.stringify(data);
  },
  pageQueryOp:{
    // // 关联表用户表
    // leftJoin: [{
    //   entity: courseSourceEntity,
    //   alias: 'b',
    //   condition: 'a.phase_name = b.id'
    // }],
    where: async (ctx: Context) => { 
      return [ 
        ['a.isDelete = :isDelete', { isDelete: false }] 
      ] 
    },
    // 添加排序
    // addOrderBy: {
    //   createTime: "desc"
    // }
  },
  listQueryOp:{ 
    // 让type字段支持筛选
    // fieldEq: ['course_type'],
    select: ['a.*','b.student','e.name as teacher_name','c.section_theme as course_section_theme','c.learning_goals','d.phase_name','b.isLeave','b.rate','b.comments','b.common_use_num','b.special_use_num'],
     // 关联表用户表
    leftJoin: [{
      entity: studentClassDetailEntity,
      alias: 'b',
      condition: 'a.id = b.class_teaching'
    },{
      entity: courseSourceEntity,
      alias: 'c',
      condition: 'a.course_type = c.course_type'
    },{
      entity: courseSystemEntity,
      alias: 'd',
      condition: 'c.phase_name = d.id'
    },{
      entity: teacherEntity,
      alias: 'e',
      condition: 'a.teacher = e.id'
    }],
    where: async (ctx: Context) => { 
      const body_data: any= JSON.parse(ctx.request.body.data)
      console.log(body_data,'111111111');
      let ret:any = [
        ['a.isDelete = :isDelete', { isDelete: false }]
      ]
      if (body_data.student_id){
        ret.push(['b.student = :studentId', { studentId: body_data.student_id }])
      }
      return ret
    },
    // 添加排序
    // addOrderBy: {
    //   createTime: "desc"
    // }
  }
})
export class classTeachingAdminController extends BaseController {

  @Inject()
  service:BaseService

  @Post('/classStudentsClassDetail')
  async classCourseStudents(@Body(ALL) queryObject: any) {
    console.log(queryObject,11111111);
    // 请求体中获取学生id
    const {id} = queryObject
    
    const info = await this.service.nativeQuery(
      `
        select b.name,a.class_teaching,f.name as teacherName,d.learning_goals,e.classgroupName,d.section_theme,a.note,a.common_use_num,a.special_use_num,a.isLeave,a.isPlacement,a.comments,a.rate,c.class_date,d.phase_name from student_class_detail a
        left join student b on a.student = b.id
        left join class_teaching c on a.class_teaching = c.id
        left join (select a.id,concat(b.course_id,'-',a.section_theme) as section_theme,a.learning_goals,c.phase_name from course_source a
          left join course_type b on a.course_type = b.id
          left join course_system c on a.phase_name = c.id
          ) d on c.section_theme = d.id
        left join (select a.id,concat(a.class_date,a.class_name,a.class_number) as classgroupName from classgroup a
          left join classgroup_students b on a.id = b.classgroup_id where b.student_id = ${id}) e on c.teaching_class = e.id
        left join teacher f on c.teacher = f.id
          where a.student = ${id} and a.isDelete = false and c.isDelete = false and a.isLeave = false
      `
    )
 
    return {code:1000,data:info}
  }

}
